package com.jeesite.modules.sys.wx.qy.util;

import com.github.sd4324530.fastweixin.company.api.QYOauthAPI;
import com.github.sd4324530.fastweixin.company.api.QYUserAPI;
import com.github.sd4324530.fastweixin.company.api.config.QYAPIConfig;
import com.github.sd4324530.fastweixin.company.api.entity.QYUser;
import com.github.sd4324530.fastweixin.company.api.enums.QYResultType;
import com.github.sd4324530.fastweixin.company.api.response.GetOauthUserInfoResponse;
import com.jeesite.common.web.CookieUtils;
import com.jeesite.modules.sys.entity.Employee;
import com.jeesite.modules.sys.entity.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Lazy(false)
//@PropertySource("config/qy.properties")
public class QyConfigUtil {

    public static String corpId;

    public static String agentId;

    //通讯录txSecret
    public static String txSecret;

    //应用secret
    public static String corpSecret;


    public static QYAPIConfig apiConfig;

    //获取通讯录的apiConfig
    public static QYAPIConfig txApiConfig;


    @Value("${corpId}")
    public void setCorpId(String corpId) {
        QyConfigUtil.corpId = corpId;
    }

    @Value("${agentId}")
    public void setAgentId(String agentId) {
        QyConfigUtil.agentId = agentId;
    }

    @Value("${corpSecret}")
    public void setCorpSecret(String corpSecret) {
        QyConfigUtil.corpSecret = corpSecret;
    }

    @Value("${txSecret}")
    public void setTxSecret(String txSecret) {
        QyConfigUtil.txSecret = txSecret;
    }


    public static QYAPIConfig getQYAPIConfig() {
        if (apiConfig == null) {
            apiConfig = new QYAPIConfig(QyConfigUtil.corpId, QyConfigUtil.corpSecret,true);
        }
        return apiConfig;

    }

    public static QYAPIConfig getTXQYAPIConfig() {
        if (txApiConfig == null) {
            txApiConfig = new QYAPIConfig(QyConfigUtil.corpId, QyConfigUtil.txSecret);
        }
        return txApiConfig;

    }

    public static QYUserAPI getQyUserAPI() {
        QYAPIConfig apiConfig = getQYAPIConfig();
        QYUserAPI qyUserAPI = new QYUserAPI(apiConfig);
        return qyUserAPI;
    }


    public static QYOauthAPI getQYOauthAPI() {
        QYAPIConfig apiConfig = getQYAPIConfig();
        QYOauthAPI qyOauthAPI = new QYOauthAPI(apiConfig);
        return qyOauthAPI;
    }

    //获取accessToken
    public static String getAccessToken() {
        QYAPIConfig apiConfig = getQYAPIConfig();
        String token = apiConfig.getAccessToken();
        return token;
    }



    //创建用户
    public static QYResultType createUser(User user) {
        Employee employee = (Employee)user.getRefObj();
        String department = employee.getOffice().getViewCode();


        Integer[] a = new Integer[1];
        a[0]=Integer.parseInt(department);

        //封装user
        QYUser qyUser = new QYUser();
        qyUser.setUserId(user.getLoginCode());
        qyUser.setName(user.getUserName());
        qyUser.setMobile(user.getMobile());
        qyUser.setDepartment(a);

        QYAPIConfig qyapiConfig = getTXQYAPIConfig();

        //获取api 并进行调取
        QYUserAPI qyUserAPI = new QYUserAPI(qyapiConfig);
        QYResultType qyResultType = qyUserAPI.create(qyUser);

        return qyResultType;
    }

    public static QYResultType updateUser(User user) {
        QYUser qyUser = new QYUser();

        Employee employee = (Employee)user.getRefObj();
        String department = employee.getOffice().getOfficeCode();

        Integer[] a = new Integer[1];
        a[0]=Integer.parseInt(department);
        qyUser.setUserId(user.getLoginCode());
        qyUser.setName(user.getUserName());
        qyUser.setMobile(user.getMobile());
        qyUser.setDepartment(a);


        QYAPIConfig qyapiConfig = getTXQYAPIConfig();

        //获取api 并进行调取
        QYUserAPI qyUserAPI = new QYUserAPI(qyapiConfig);
        QYResultType qyResultType = qyUserAPI.update(qyUser);
        return qyResultType;
    }


    public static String getUserId(HttpServletRequest request, HttpServletResponse response) {
        //通过code值获取openid
        QYUserAPI oauthAPI = QyConfigUtil.getQyUserAPI();

        String code = request.getParameter("code");
        String userId =  CookieUtils.getCookie(request,"userId");
        //为空代表从原地址进来,
        if (StringUtils.isEmpty(userId)) {
            if (StringUtils.isEmpty(code)) {
                userId = request.getParameter("userId");
                if (!StringUtils.isEmpty(userId)) {
                    CookieUtils.setCookie(response, "userId", userId);
                }
            }else{ //不为空说明 重定向 网页授权
                GetOauthUserInfoResponse userInfoResponse = oauthAPI.getOauthUserInfo(code);
                userId = userInfoResponse != null ? userInfoResponse.getUserid() : "";
                CookieUtils.setCookie(response,"userId",userId);
            }
        }
        return userId;
    }



}